package com.example.tliaswebmanagement.controller;

import com.example.tliaswebmanagement.entity.Clazz;
import com.example.tliaswebmanagement.entity.PageResult;
import com.example.tliaswebmanagement.entity.Result;
import com.example.tliaswebmanagement.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@RequestMapping("/clazzs")
@RestController
public class ClazzController {
    private final ClazzService clazzService;
    @Autowired
    public ClazzController(ClazzService clazzService) {
        this.clazzService = clazzService;
    }
    @GetMapping
    public Result findAll(@RequestParam(defaultValue = "1")Integer page,
                          @RequestParam(defaultValue = "10") Integer pageSize,
                          String name,
                          @DateTimeFormat(pattern="yyyy-MM-dd") LocalDate begin,
                          @DateTimeFormat(pattern="yyyy-MM-dd")LocalDate end){
        log.info("分页查询，参数：page={},pageSize={},name={},begin={},end={}",page,pageSize,name,begin,end);
        PageResult pr=clazzService.findAll(page,pageSize,name,begin,end);
        return Result.success(pr);
    }
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("删除班级，id={}",id);
        clazzService.deleteById(id);
        return Result.success();
    }
    @PostMapping
    public Result add(@RequestBody Clazz clazz){
        log.info("添加班级，clazz={}",clazz);
        clazzService.insert(clazz);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        log.info("查询班级id为：{}",id);
        Clazz clazz=clazzService.findById(id);
        return Result.success(clazz);
    }
    @PutMapping
    public Result update(@RequestBody Clazz clazz){
        log.info("修改班级，clazz={}",clazz);
        clazzService.update(clazz);
        return Result.success();
    }

    @GetMapping("/list")
    public Result getAll(){
        log.info("查询所有班级");
        List<Clazz> clazzs=clazzService.getAll();
        return Result.success(clazzs);
    }
}
